
package me.fanchaoo.web.util;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.mail.javamail.JavaMailSender;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import me.fanchaoo.dbo.EmailDBO;

@SuppressWarnings("unchecked")
@Component
public class EmailUtils {

    /**
     * 邮箱号
     */
    @Value("${spring.mail.username}")
    private String email;

    @Autowired
    private JavaMailSender javaMailSender;

    public  void  send(EmailDBO data) throws MessagingException {
        // 构建一个邮件对象
        MimeMessage message = javaMailSender.createMimeMessage();
        // 获取邮件实体类信息
        EmailDBO emailDTO = data;
        // 设置邮件主题
        message.setSubject(emailDTO.getSubject());
        MimeMessageHelper messageHelper = new MimeMessageHelper(message,true,"utf-8");
        // 设置邮件发送者
        messageHelper.setFrom(email);
        // 设置邮件接收者
        messageHelper.setTo(emailDTO.getEmail());
        // 设置邮件正文
        messageHelper.setText(emailDTO.getContent(),true); //true 代表发送html格式
        // 发送邮件
        javaMailSender.send(message);
    }


}
